import request from "@/utils/request";

// 登录方法
export async function login(username, password, flag) {
  // const data = {
  //   username,
  //   password,
  // };
  // return request({
  //   url: "/login",
  //   headers: {
  //     isToken: false,
  //     repeatSubmit: false,
  //   },
  //   method: "post",
  //   data: data,
  // });
  const response = await request({
    url: `${window.location.origin}/${username}.json`,
    method: "get",
  })

  // 特殊操作返回
  if (flag) {
    if (response !== undefined) return {
      accessToken: username
    }
    return Promise.reject(false)
  }

  // 正常登录返回
  if (password + '' === response + '') return {
    accessToken: username
  }
  return Promise.reject(false)

}

// 注册方法
export function register(data) {
  return request({
    url: "/register",
    headers: {
      isToken: false,
    },
    method: "post",
    data: data,
  });
}

// 获取用户详细信息
export function getInfo() {
  // return request({
  //   url: '/getInfo',
  //   method: 'get'
  // })
  return new Promise((resolve) => {
    resolve({
      msg: "操作成功",
      code: 200,
      permissions: ["*:*:*"],
      roles: ["admin"],
      user: {
        createBy: "admin",
        createTime: "2023-04-23 16:11:38",
        updateBy: null,
        updateTime: null,
        remark: "管理员",
        userId: 1,
        deptId: 103,
        userName: "admin",
        nickName: "若依",
        email: "ry@163.com",
        phonenumber: "15888888888",
        sex: "1",
        avatar: "",
        password: "$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2",
        status: "0",
        delFlag: "0",
        loginIp: "101.247.128.187",
        loginDate: "2023-11-18T22:53:38.000+08:00",
        dept: {
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          deptId: 103,
          parentId: 101,
          ancestors: "0,100,101",
          deptName: "研发部门",
          orderNum: 1,
          leader: "若依",
          phone: null,
          email: null,
          status: "0",
          delFlag: null,
          parentName: null,
          children: [],
        },
        roles: [
          {
            createBy: null,
            createTime: null,
            updateBy: null,
            updateTime: null,
            remark: null,
            roleId: 1,
            roleName: "超级管理员",
            roleKey: "common",
            roleSort: 1,
            dataScope: "1",
            menuCheckStrictly: false,
            deptCheckStrictly: false,
            status: "0",
            delFlag: null,
            flag: false,
            menuIds: null,
            deptIds: null,
            permissions: null,
            admin: true,
          },
        ],
        roleIds: null,
        postIds: null,
        roleId: null,
        admin: true,
      },
    });
  });
}

// 退出方法
export function logout() {
  return new Promise((resolve) => {
    resolve({
      msg: "操作成功",
      code: 200,
    });
  });
}

// 获取验证码
export function getCodeImg() {
  return request({
    url: "/captchaImage",
    headers: {
      isToken: false,
    },
    method: "get",
    timeout: 20000,
  });
}
